package com.wcards.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wcards.config.R;
import com.wcards.dto.CartsDTO;
import com.wcards.entity.Carts;
import com.wcards.mapper.CartsMapper;
import com.wcards.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class CartServiceImpl extends ServiceImpl<CartsMapper, Carts> implements CartService {
    @Resource
    private CartsMapper cartsMapper;

    /*
     *     添加到购物车
     * */
    @Override
    public R<String> addCart(Carts carts) {
        cartsMapper.insert(carts);
        return R.success("添加成功！");
    }

    /*
     * 删除购物车
     * */
    @Override
    public R<String> deleteById(List<Integer> ids) {

        cartsMapper.deleteBatchIds(ids);
        return R.success("删除成功！");
    }

    /*
     *购物车修改
     * */

    @Override
    public R<String> updateCart(Carts carts) {
        cartsMapper.updateById(carts);
        return R.success("修改成功！");
    }
    /*
     * 查询购物车
     * */

    @Override
    public R<Page<Carts>> findCates(CartsDTO cartsDTO, Integer pageSize, Integer pageNumber) {
        Page<Carts> cartsPage = new Page(pageSize, pageNumber);
        LambdaQueryWrapper<Carts> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Carts::getUserId, cartsDTO.getUserId());
        cartsMapper.selectPage(cartsPage, lqw);
        return R.success(cartsPage);
    }
}
